package com.catarc.unityback.action;

import com.catarc.unityback.entity.cmd.ActionModelCmd;
import com.catarc.unityback.entity.cmd.LoginModelCmd;
import com.catarc.unityback.entity.exception.UnityBackException;
import com.catarc.unityback.entity.vo.MyPerson;
import com.iohao.game.action.skeleton.annotation.ActionController;
import com.iohao.game.action.skeleton.annotation.ActionMethod;
import com.iohao.game.action.skeleton.core.flow.FlowContext;
import com.iohao.game.bolt.broker.client.kit.UserIdSettingKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author van
 * @date 2023/2/26 11:49
 */
@Slf4j
@Component
@ActionController(ActionModelCmd.LOGIN_MODEL)
public class LoginPanelAction {

    public static Map<Long, MyPerson> personMap = new ConcurrentHashMap<>();

    @ActionMethod(LoginModelCmd.LOGIN)
    public MyPerson login(MyPerson person, FlowContext flowContext) {
        assert person.getName() != null;
        person.setId(personMap.size() + 1L);
        // channel 中设置用户的真实 userId；
        boolean success = UserIdSettingKit.settingUserId(flowContext, person.getId());

        if (!success) {
            log.error("未能成功设置用户");
            throw new UnityBackException("未能成功设置用户");
        }

        return person;
    }
}
